package ipfms.stripes.maintenance;

import able.stripes.JSP;
import com.google.inject.Inject;
import ipfms.dao.BuildingDAO;
import ipfms.dao.FaultyReportDAO;
import ipfms.model.*;
import ipfms.stripes.BaseActionBean;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author haqqiem
 */

@UrlBinding("/check_complaint")
public class CheckingComplaintsActionBean extends BaseActionBean{
    private static final Logger logger = Logger.getLogger(CheckingComplaintsActionBean.class);

    private Pengguna pengguna = new Pengguna();
    private FaultyReport fReport = new FaultyReport();
    private Building building = new Building();
    private String selectedTab = "0";
    private String frm = null;

    List<FaultyReport> listOfReport = new ArrayList<FaultyReport>();

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;

    @Inject
    private FaultyReportDAO fReportDAO;

    @Inject
    private BuildingDAO buildingDAO;

    @DefaultHandler
    public Resolution showForm() {
        pengguna = getContext().getCurrentUser();

        Session session = sessionProvider.get();
        Query query = session.createQuery(
                "SELECT fr from FaultyReport fr " +
                "WHERE fr.infoAudit.dimasukOleh = :user");
        query.setString("user", pengguna.getIdPengguna());

        listOfReport = query.list();
        logger.info("listOfReport.size() : "+listOfReport.size());

        return new JSP("maintenance/check_complaints.jsp");
    }

    public Resolution searching(){
        logger.info("fReport.getIdReport() : "+fReport.getIdReport());
        int id = fReport.getIdReport();

        fReport = new FaultyReport();
        listOfReport = new ArrayList<FaultyReport>();

        fReport = fReportDAO.findById(id);
        if(fReport!=null){
            listOfReport.add(fReport);
        }
        logger.info("listOfReport.size() : "+listOfReport.size());

        return new JSP("maintenance/check_complaints.jsp");
    }

    public Resolution viewDetail() {
        String idBuilding = getContext().getRequest().getParameter("idBuilding");
        String idReport = getContext().getRequest().getParameter("idReport");
        frm = getContext().getRequest().getParameter("from");
        logger.info(idBuilding+" "+idReport+" "+frm);
        if (StringUtils.isNotBlank(idBuilding)) {
            building = buildingDAO.findById(Integer.valueOf(idBuilding));
        }
        if (StringUtils.isNotBlank(idReport)) {
            fReport = new FaultyReport();
            fReport = fReportDAO.findById(Integer.valueOf(idReport));
        }
        return new JSP("maintenance/tab_maintenence.jsp");
    }

    public Resolution back() {
        return showForm();
    }

    public Resolution backList() {
        return new RedirectResolution(ReportListActionBean.class);
    }

    public FaultyReport getfReport() {
        return fReport;
    }

    public void setfReport(FaultyReport fReport) {
        this.fReport = fReport;
    }

    public List<FaultyReport> getListOfReport() {
        return listOfReport;
    }

    public void setListOfReport(List<FaultyReport> listOfReport) {
        this.listOfReport = listOfReport;
    }

    public Pengguna getPengguna() {
        return pengguna;
    }

    public void setPengguna(Pengguna pengguna) {
        this.pengguna = pengguna;
    }

    public Building getBuilding() {
        return building;
    }

    public void setBuilding(Building building) {
        this.building = building;
    }

    public String getSelectedTab() {
        return selectedTab;
    }

    public void setSelectedTab(String selectedTab) {
        this.selectedTab = selectedTab;
    }

    public String getFrm() {
        return frm;
    }

    public void setFrm(String frm) {
        this.frm = frm;
    }

}
